use crate::config::database;
use sqlx::MySqlPool;
use std::env;
use tokio::sync::OnceCell;

pub mod config;
pub mod service;
pub mod model;
pub mod router;
pub mod error;
pub mod macros;

// 全局静态变量
static DB_POOL: OnceCell<MySqlPool> = OnceCell::const_new();

pub struct GlobalInfra;

impl GlobalInfra {
    pub async fn init() -> Result<(), Box<dyn std::error::Error>> {
        // 初始化数据库连接池
        dotenvy::dotenv().ok();
        // MySQL数据库连接池
        let database_url = env::var("DATABASE_URL")
            .map_err(|_| "DATABASE_URL environment variable not set")?;

        let pool = database::create_db_pool(&database_url).await?;
        DB_POOL.set(pool).unwrap();

        Ok(())
    }

    // 获取全局实例
    pub fn db() -> &'static MySqlPool {
        DB_POOL.get().expect("Database pool not initialized")
    }
}



